- Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathDungeonGame.java
129 lines (94 loc) Β· 3.13 KB
/
DungeonGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
packagesection19_DynamicProgramming;
publicclassDungeonGame {
publicstaticvoidmain(String[] args) {
int[][] dungeon = { { -2, -3, 3 }, { -5, -10, 1 }, { 10, 30, -5 } };
intcurrRow = 0, currCol = 0;
intendRow = dungeon.length - 1, endCol = dungeon[0].length - 1;
System.out.println(knightMinHealthRecursion(dungeon, currRow, currCol, endRow, endCol));
// top down DP
int[][] storage = newint[endRow + 1][endCol + 1];
System.out.println(knightMinHealthTopDownDP(dungeon, 0, 0, endRow, endCol, storage));
// bottom up DP
System.out.println(knightMinHealthBottomUpDP(dungeon));
}
publicstaticintknightMinHealthRecursion(int[][] dungeon, intcr, intcc, inter, intec) {
if (cr > er || cc > ec) {
returnInteger.MAX_VALUE;
}
if (cr == er && cc == ec) {
if (dungeon[cr][cc] <= 0) {
return1 - dungeon[cr][cc];
}
return1;
}
intmoveRight = knightMinHealthRecursion(dungeon, cr, cc + 1, er, ec);
intmoveBottom = knightMinHealthRecursion(dungeon, cr + 1, cc, er, ec);
intminPath = Math.min(moveRight, moveBottom);
inthealthAlreadyHave = minPath - dungeon[cr][cc];
intminHealthRequired;
if (healthAlreadyHave > 0) {
minHealthRequired = healthAlreadyHave;
} else {
minHealthRequired = 1;
}
returnminHealthRequired;
}
publicstaticintknightMinHealthTopDownDP(int[][] dungeon, intcr, intcc, inter, intec, int[][] storage) {
if (cr > er || cc > ec) {
returnInteger.MAX_VALUE;
}
if (cr == er && cc == ec) {
if (dungeon[cr][cc] <= 0) {
return1 - dungeon[cr][cc];
}
return1;
}
if (storage[cr][cc] != 0) {
returnstorage[cr][cc];
}
intmoveRight = knightMinHealthTopDownDP(dungeon, cr, cc + 1, er, ec, storage);
intmoveBottom = knightMinHealthTopDownDP(dungeon, cr + 1, cc, er, ec, storage);
intminPath = Math.min(moveRight, moveBottom);
inthealthAlreadyHave = minPath - dungeon[cr][cc];
intminHealthRequired;
if (healthAlreadyHave > 0) {
minHealthRequired = healthAlreadyHave;
} else {
minHealthRequired = 1;
}
storage[cr][cc] = minHealthRequired;
returnminHealthRequired;
}
publicstaticintknightMinHealthBottomUpDP(int[][] dungeon) {
intn = dungeon.length;
int[][] storage = newint[n + 1][n + 1];
for (introw = n; row >= 0; row--) {
for (intcol = storage[row].length - 1; col >= 0; col--) {
// base cases
if (row == storage.length - 1 || col == storage[row].length - 1) {
storage[row][col] = Integer.MAX_VALUE;
} elseif (row == storage.length - 2 && col == storage[row].length - 2) {
intval = 1;
if (dungeon[row][col] < 0) {
val = -dungeon[row][col] + 1;
}
storage[row][col] = val;
} else {
// logic from top down
intmoveRight = storage[row][col + 1];
intmoveBottom = storage[row + 1][col];
intminPath = Math.min(moveRight, moveBottom);
inthealthAlreadyHave = minPath - dungeon[row][col];
intminHealthRequired;
if (healthAlreadyHave > 0) {
minHealthRequired = healthAlreadyHave;
} else {
minHealthRequired = 1;
}
storage[row][col] = minHealthRequired;
}
}
}
returnstorage[0][0];
}
}